#!/usr/bin/env python3
# encoding: utf-8

"""
@version: 1.0
@author: 周永波
@file: setting.py
@time: 2020/8/4 10:33
"""
import logging

from demoApp.util.appUtils import dict_get
from demoApp.util.appUtils import yaml_get

log = logging.getLogger(__name__)

class ServerSetting:

    def __init__(self, property={}) -> None:
        self.host = dict_get(property, ('server', 'host'), default='127.0.0.1')
        self.port = dict_get(property, ('app', 'server', 'port'), default=8080)

class property:

    serverSetting = None

    def __init__(self, filePath: str):
        self.filePath = filePath.strip()
        self.setContent = yaml_get(filePath + "/app.yml")
        if self.setContent is None:
            raise RuntimeError(filePath + "加载错误")
        log.info(str(self.setContent))
        self.includePath = dict_get(self.setContent, ('app', 'profile', 'includePath'))
        if self.includePath is None:
            self.includePath = dict_get(self.setContent, ('app', 'profile', 'active'))
        # // 配置文件
        self.properties = self.setContent
        self.merge(active=self.active(self.setContent), includeArr=self.include(self.setContent))
        log.info("配置文件信息:" + str(self.properties))
        self.serverSetting = ServerSetting(self.properties)
        pass

    # 环境配置文件
    def active(self, dictObj):
        active = dict_get(dictObj, ('app', 'profile', 'active'))
        if active is None:
            return
        activePath = self.filePath + "/app-" + active.strip() + ".yml"
        log.info("加载配置文件" + activePath)
        activeContext = yaml_get(activePath)
        if activeContext is None:
            log.warning("加载配置文件" + activePath + "错误")
            return
        log.info("配置文件" + activePath + ";context=" + str(activeContext))
        return activeContext

    # 引入的配置文件
    def include(self, dictObj):
        includeStr = dict_get(dictObj, ('app', 'profile', 'include'))
        if includeStr is None:
            return
        includeArr = includeStr.split(";")
        includeProperties = []
        for include in includeArr:
            includePath = self.filePath + "/app-" + include.strip() + ".yml"
            if self.includePath is not None and self.includePath.strip() != '':
                includePath = self.filePath + "/" + self.includePath + "/app-" + include.strip() + ".yml"
            log.info("加载配置文件" + includePath)
            yamlVal = yaml_get(includePath)
            if yamlVal is None:
                log.warning("加载配置文件" + includePath + "错误")
                continue
            log.info("配置文件" + includePath + ";context=" + str(yamlVal))
            includeProperties.append(yamlVal)
        return includeProperties

    # 将所有的配置读取且进行合并
    def merge(self, active={}, includeArr=[]):

        # 合并读取active
        if active is not None:
            self.properties = dict(self.properties, **active)
            self.merge(active=self.active(active), includeArr=self.include(active))

        if includeArr is not None and len(includeArr) > 0:
            for item in includeArr:
                self.properties = dict(self.properties, **item)
                self.merge(active=self.active(item), includeArr=self.include(item))
        pass


